<?php
class sfBreadcrumbModule {
	
	private $sections = array();
	
	private $symfony_name;
	
	private $display_name;
	
	private $url = null;
	
	public function __construct($symfony_name, $display_name) {
		
		$this->symfony_name = $symfony_name;
		
		$this->display_name = $display_name;
		
	}
	
	public function getLink() {
		
		// load required helpers
		sfContext::getInstance()->getConfiguration()->loadHelpers(array('Breadcrumbs', 'Url', 'Tag', 'I18N'));
		
		$link = null;
		
		if ($this->url != null) {
			
			$link = content_tag('a', __($this->display_name), array('href' => $this->url,));
			
		} else {
			
			$link = link_to( __($this->display_name), $this->symfony_name.'/index' );
			
		}
		
		
		return $link;
		
	}
	
	/**
	* Returns the symfony name of this module
	*
	* @return string symfony name
	*/
	public function getSymfonyName() {
		
		return $this->symfony_name;
		
	}
	
	
	public function setSymfonyName($symfony_name) {
		
		if(is_string($symfony_name)) {
			
			$this->symfony_name = $symfony_name;
			
		}
		
	}
	
	/**
	* Returns the display name of this module
	*
	* @return string display name
	*/
	public function getDisplayName() {

		return $this->display_name;

	}

	public function setDisplayName($display_name) {

		if(is_string($display_name)) {

			$this->display_name = $display_name;

		}

	}
	
	
	/**
	* Returns an array of this module's sections
	* 
	* @return array Current module's sections
	*/
	public function getSections() {
		
		return $this->sections;
		
	}
	
	
	/**
	* Retrieves a sfBreadcrumbSection object
	* 
	* @param string section symfony name
	* @return sfBreadcrumbSection The sfBreadcrumbSection object
	*/
	public function getSection($section_name) {
		
		foreach($this->sections as $section){
			
			if( $section->getSymfonyName() == $section_name ){
			
				return $section;
			
			}
			
		}
		
	}
	
	/**
	* Sets this modules sections
	*
	* @param array An array of sfBreadcrumbSection's objects
	*/
	public function setSections(array $sections) {
		
		$this->sections = $sections;
		
	}
	
	/**
	* Returns the section in which an action is located
	*
	* @param sfBreadcrumbAction a sfBreadcrumbAction object
	* @return sfBreadcrumbSection a sfBreadcrumbSection object
	*/
	public function findSection($action_name) {

		foreach($this->sections as $section) {
			
			foreach($section->getActions() as $action) {
				
				if( $action->getSymfonyName() == $action_name ){
					
					return $section;
					
				}
				
			}
			
		}
		
	}
	
	public function getUrl() {
		
		return $this->url;
		
	}
	
	public function setUrl($url) {
		
		if (is_string($name)) {
			
			$this->url = $url;
			
		}
		
	}
	
}
